package com.example.administrator.yunniuusedcar.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.administrator.common.base.BaseFragment;
import com.example.administrator.common.base.Global;
import com.example.administrator.common.base.MyApp;
import com.example.administrator.yunniuusedcar.MainActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.activity.CommodityDetailsActivity;
import com.example.administrator.yunniuusedcar.adapter.PingXing_XiaoLiangAdapter2;
import com.example.administrator.yunniuusedcar.adapter.SelectCarAdapter;
import com.example.administrator.yunniuusedcar.adapter.baseadapter.BaseAdapter;
import com.example.administrator.yunniuusedcar.bean.SelectCarData;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.model.mode_benn.PingXing_XiaoLiang_Bean;
import com.example.administrator.yunniuusedcar.refresh.PullRefreshLayout;
import com.example.administrator.yunniuusedcar.util.Judge;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/** 平进豪车 销量查询 (更新的)
 * Created by Administrator on 2017/12/29.
 */
@SuppressLint("ValidFragment")
public class PingJingXiaoLiangFragment extends BaseFragment{

        private int pager = 1;
        private PullRefreshLayout mPull;
        private SwipeRefreshLayout swipeRefreshLayout;
        private RecyclerView recyclerView8;
        private PingXing_XiaoLiangAdapter2 adapter;
        List<PingXing_XiaoLiang_Bean> dataList =new ArrayList<>();
    private PingXing_XiaoLiang_Bean bean;

    @SuppressLint("ValidFragment")
        public PingJingXiaoLiangFragment() {
        }


        @Override
        public int getLayoutRes() {
            return R.layout.item_recycleview2;
        }

        @Override
        public void initView() {
            swipeRefreshLayout = findView(R.id.swipelayout);
            mPull =  findView(R.id.pull);
            initRecycleV();
        }
        private void initRecycleV() {

            recyclerView8 = findView(R.id.recycler_view6);

            recyclerView8.setLayoutManager(new LinearLayoutManager(mActivity));

            //创建数据
            listDatas = new ArrayList();

//        zhongGuiCarZongHeAdapter1 = new ZhongGuiCar_zongHeAdapter(getContext(),
//                R.layout.fragment_zonghepaixu,listDatas);
            adapter = new PingXing_XiaoLiangAdapter2(getContext());
            recyclerView8.setAdapter(adapter);


            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    dataList.clear();
                    pager =1;
                    initUloadMenuInfor2();
                }
            });


            mPull.setOnPullListener(new PullRefreshLayout.OnPullListener() {
                @Override
                public void onLoadMore(final PullRefreshLayout pullRefreshLayout) {
                    pager = pager+1;
                    initUloadMenuInfor2();
                    pullRefreshLayout.setRefreshing(false);
                }
            });

/////
        }
        @Override
        public void initListener() {
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                 //   int SuiJiCarID =  dataList.get(position).car_id;    //平进豪车 综合排序id

                    Intent intent = new Intent(MainActivity.mactivity, CommodityDetailsActivity.class);   // 商品详情
                    intent.putExtra("RanCarid",bean.data.get(position).car_id);

                    MainActivity.mactivity.startActivity(intent);
                }
            });
        }

        @Override
        public void initData() {
            initUloadMenuInfor2();    //// (  加载数据 )
        }

        static ArrayList listDatas = new ArrayList();

        private void initUloadMenuInfor2() {    // 加载数据
//            if(MyApp.enter_type.equals("typeCar")){
//
//                String ul = IHttpService.URL2 +IHttpService.GETBYCARTYPELIS ;    // 根据车辆类型选车

            String ul = IHttpService.HOST_URL + "Car/typeCar";

                OkHttpUtils.post().url(ul).addParams("sign",IHttpService.SignVal).addParams("page",pager+"").addParams("num","3")
                        .addParams("type","2") .addParams("type_id","7").build().execute(new StringCallback() {     //.addParams("type","1")   销量排序

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Global.showToast("请求超时！"+ e.getMessage());
                        //      System.out.println("-----====----------A"+ e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        handlerData(response);
                    }
                });
//            }else if(MyApp.enter_type.equals("termCar")){
//                String ul = IHttpService.URL2 +IHttpService.GETSEARCHCARLIST ;  //根据条件选车
            String ul2 = IHttpService.HOST_URL + "Car/termCar";

                OkHttpUtils.post().url(ul2).addParams("sign",IHttpService.SignVal).addParams("page",pager+"").addParams("num","3")
                        .addParams("type","2")
                        .addParams("type_id", MyApp.type_id+"")
                        .addParams("brand_id",MyApp.brand_id+"")
                        .addParams("level_id",MyApp.level_id+"")
                        .addParams("price_id",MyApp.price_id+"")
                        .build().execute(new StringCallback() {     //.addParams("type","1")   价格排序

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Global.showToast("中规豪车、价格排序，请求超时！"+ e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        handlerData(response);
                    }
                });
            }

        /**
         * 处理data
         * @param response
         */
        private void handlerData(String response) {
            try {
                swipeRefreshLayout.setRefreshing(false);
                JSONObject jsonObject = new JSONObject(response);
                String data = jsonObject.getString("data");
                dataList .addAll(getDataList(data));
                adapter.refreshData(dataList);
                //recyclerView7.refreshComplete();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        private List<PingXing_XiaoLiang_Bean> getDataList(String data_str) {
            if (!Judge.isJson(data_str)) {
                return null;
            }

            List<PingXing_XiaoLiang_Bean> datas = new ArrayList<>();
            try {
                JSONArray array = new JSONArray(data_str);

                for (int i = 0; i < array.length(); i++) {
                    bean = new Gson().fromJson(array.get(i).toString(), PingXing_XiaoLiang_Bean.class);
                    datas.add(bean);
                }
                return datas;
            } catch (JSONException e) {
                return null;
            }
        }
    }



//    private RecyclerView recyclerView7;
//    private PingXing_XiaoLiangAdapter2 pingXingXiaoLiangAdapter2;
//    ArrayList listDatas = new ArrayList();
//
//    @Override
//    public int getLayoutRes() {
//        return R.layout.item_recycleview2;  //fragment_zonghepaixu
//
//    }
//    @Override
//    public void initView() {
//        initUloadMenuInfor();
//        initRecycleV();
//    }
//    private void initRecycleV() {
//
//        recyclerView7 = findView(R.id.recycler_view6);
//
//        recyclerView7.setLayoutManager(new LinearLayoutManager(mActivity));
//
//        //创建数据
//        listDatas = new ArrayList();
//
////        zhongGuiCarZongHeAdapter1 = new ZhongGuiCar_zongHeAdapter(getContext(),
////                R.layout.fragment_zonghepaixu,listDatas);
//        pingXingXiaoLiangAdapter2 = new PingXing_XiaoLiangAdapter2(getContext(),listDatas);
//        recyclerView7.setAdapter(pingXingXiaoLiangAdapter2);
//        initUloadMenuInfor();    //// (  加载数据 )
///////
//    }
//
//    @Override
//    public void initListener() {
//
//    }
//
//    @Override
//    public void initData() {
//
//    }
//    private void initUloadMenuInfor() {    // 加载数据
//
//        String ul = IHttpService.HOST_URL + "Car/typeCar";    // 销量查询
//
//        OkHttpUtils.post().url(ul).addParams("sign",IHttpService.SignVal).addParams("page","1").addParams("num","3")
//                .addParams("type","2").addParams("type_id","7").build().execute(new StringCallback() {     //.addParams("type","1")   销量查询
//
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                Global.showToast("平行 销量，请求超时！"+ e.getMessage());
//                System.out.println("-----====----------A"+ e);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                ////        Global.showToast("====="+response);
//                System.out.println("-----====-----------A1"+response);
//                Gson js = new Gson();
//                PingXing_XiaoLiang_Bean xlArticleInfoBean = js.fromJson(response,PingXing_XiaoLiang_Bean.class);
//                listDatas.add(xlArticleInfoBean);
////                ZhongGuiCar_zongHeAdapter zhongGuiCarZongHeAdapter = new ZhongGuiCar_zongHeAdapter();
////                zhongGuiCarZongHeAdapter.set
//                pingXingXiaoLiangAdapter2.notifyDataSetChanged();
//            }
//        });
//    }

